﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;

namespace CodeJumper
{
    class ProjectHelper
    {
        public static string GetFilePath(Microsoft.VisualStudio.Text.Editor.IWpfTextView wpfTextView)
        {
            Microsoft.VisualStudio.Text.ITextDocument document;
            if ((wpfTextView == null) ||
                    (!wpfTextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(Microsoft.VisualStudio.Text.ITextDocument), out document)))
                return String.Empty;

            // If we have no document, just ignore it.
            if ((document == null) || (document.TextBuffer == null))
                return String.Empty;

            return document.FilePath;
        }

        public static ProjectItem GetProjectItem(ApplicationConfiguration AppConfig, string fileName)
        {
            if (!String.IsNullOrEmpty(fileName))
            {
                if (AppConfig.HookInformation.Dte2 != null)
                {
                    var prjItem = AppConfig.HookInformation.Dte2.Solution.FindProjectItem(fileName);
                    return prjItem;
                }
            }
            return null;
        }

        public static Project GetContainingProject(ApplicationConfiguration AppConfig, string fileName)
        {
            if (!String.IsNullOrEmpty(fileName))
            {
                if (AppConfig.HookInformation.Dte2 != null)
                {
                    var prjItem = AppConfig.HookInformation.Dte2.Solution.FindProjectItem(fileName);

                    if (prjItem != null)
                        return prjItem.ContainingProject;
                }
            }
            return null;
        }

    }
}
